<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-01-04 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/ajax/master.mod.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);

		require_once ROOT_PATH.'include/lib/image.func.php';
		require_once ROOT_PATH.'include/logic/image.logic.php';

		$this->excute();
	}

	function excute()
	{
		switch($this->act)
		{
			case 'do_upload':
				$this->doUpload();
				break;
			case 'do_upload2':
				$this->doUpload2();
				break;
			case 'ftp_upload':
				$this->upload_ftp_image();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			default:
				exit();
		}
	}

	/**
	 1.海报
	 １.４８０*３２０ / 320*480
	 ２.２５５*３６５
	 ３.１０２*１４６
	 ４.８２*１１７

	 2.剧照
	 1.９0*90
	 2.280*999
	 3.320*480 / 480*３２０
	 */
	private function doUpload()
	{
		$mid = isset($this->request['mid']) ? (int)$this->request['mid'] : '';
		$cid = isset($this->request['cid']) ? (int)$this->request['cid'] : 2;
		
		if(!$mid)
		{
			js_alert_output('请指定ID');
		}

		$field = 'image';

		//------------ copy image to temp path ------------//
		$image_file = move_image_to_temp_path($field);
		
		if(!is_image($image_file))
		{
			js_alert_output('图片上传失败');
		}

		//------------ insert into database ------------//
		$image_id = insert_image_to_database($image_file, $mid, $cid);
		
		if(!$image_id || !is_numeric($image_id))
		{
			js_alert_output('插入数据库失败');
		}

		//------------ make thumb image ------------//
		make_movie_thumb_image($image_id, $image_file, $cid);

		echo "<script type='text/javascript'>";
		echo "parent.window.location.href='".BASE_URL."admin.php?m=movie&a=modify&id={$mid}';";
		echo "</script>";
	}
	
	private function doUpload2()
	{
		if(!isset($this->request['id']) || !$this->request['id'])
		{
			js_alert_output('请指定ID');
		}

		$field = 'image';

		//------------ copy image to temp path ------------//
		$image_file = move_image_to_temp_path($field);
		
		if(!is_image($image_file))
		{
			js_alert_output('图片上传失败');
		}

		//------------ insert into database ------------//
		$id = (int)$this->request['id'];
		$cid = isset($this->request['cid']) ? (int)$this->request['cid'] : 3;
		$image_id = insert_image_to_database($image_file, $id, $cid);
		
		if(!$image_id || !is_numeric($image_id))
		{
			js_alert_output('插入数据库失败');
		}

		//------------ make thumb image ------------//
		make_movie_thumb_image($image_id, $image_file, $cid);

		$url = isset($this->request['url']) ? urldecode($this->request['url']) : '';
		
		echo "<script type='text/javascript'>";
		echo "parent.window.location.href='".BASE_URL.$url."';";
		echo "</script>";
	}
	
	private function upload_ftp_image()
	{
		$mid = (int)$this->request['mid'];
		$cid = isset($this->request['cid']) ? (int)$this->request['cid'] : 2;

		$count = 0;
		$status = 'false';
		$message = '';
			
		if($cid == 1)
		{
			$message = '请选择上传剧照';
		}
		else
		{
			$ftp_image_path = ROOT_PATH.'data/ftp_images/';
			
			require_once ROOT_PATH.'lib/io.han.php';
			$IoHandler = new IoHandler();
			$files = $IoHandler->ReadDir($ftp_image_path);
			
			if(check_array($files))
			{
				foreach($files as $img)
				{
					if(is_image($img))
					{
						$status = 'true';
						$image_id = insert_image_to_database($img, $mid, $cid);
						
						if($image_id && is_numeric($image_id))
						{
							make_movie_thumb_image($image_id, $img, $cid);
							$count ++;
						}
					}
				}
				
				$IoHandler->ClearDir($ftp_image_path);
			}
			else
			{
				$message = '文件夹为空';
			}
		}
		
		$results['status'] = $status;
		$results['count'] = $count;
		$results['message'] =  $message;
		echo json_encode($results);
	}

	private function doDelete()
	{
		if(!isset($this->request['ids']) || !$this->request['ids'])
		{
			js_alert_output('请选择图片');
		}

		delete_movie_images($this->request['ids']);

		$url = isset($this->request['url']) ? urldecode($this->request['url']) : '';
		
		echo "<script type='text/javascript'>";
		echo "window.location.href='".BASE_URL.$url."';";
		echo "</script>";
	}
}